/**
 * Project Name:gc.sms.mvn
 * File Name:EmployeeServiceImpl.java
 * Package Name:com.clps.dev.sms.service.impl
 * Date:2018-11-27 16:36:38
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.service.sm.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.common.service.impl.BaseServiceImpl;
import com.clps.dev.sms.dao.sm.EmployeeDao;
import com.clps.dev.sms.entity.sm.Employee;
import com.clps.dev.sms.service.sm.IEmployeeService;
import com.clps.dev.sms.util.page.PageBean;
import com.clps.dev.sms.util.result.ResponseResultUtil;

/**
 * ClassName: EmployeeServiceImpl <br/>
 * Description: EmployeeServiceImpl.class <br/>
 * <br/>
 *
 * @author Jack.Huang
 * @version V1.0
 * @since JDK 1.8
 * @date:2018-11-27 16:36:38
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService{
	@Autowired
	private EmployeeDao employeeDao;
	
	private Logger logger = Logger.getLogger(EmployeeServiceImpl.class);
	
	public EmployeeServiceImpl() {
		super(Employee.class);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#addEmployee(com.clps.dev.sms.entity.sm.Employee)
	 */
	@Override
	public ResponseResult<Employee> addEmployee(Employee employee) {
		if (employee.getEmpName() == null || employee.getEmpPinYinName() == null || employee.getEmpEnglishName() == null
				|| employee.getEmpeMail() == null || employee.getEmpPositionId() == null
				|| employee.getEmpMobile() == null || employee.getEmpStatus() == null || employee.getEmpTel() == null
				|| employee.getEmpCreateName() == null || employee.getEmpCreateTime() == null
				|| employee.getEmpUpdatedEmployeeName() == null) {
			logger.debug(ResultType.INPUT_NULL);
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		int label = employeeDao.save(employee);
		if(label>0) {
			return ResponseResultUtil.success(ResultType.ADD_SUCCESS);
		}else {
			return ResponseResultUtil.error(ResultType.ADD_FAULT);
		}
	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#editEmployee(com.clps.dev.sms.entity.sm.Employee)
	 */
	@Override
	public ResponseResult<Employee> editEmployee(Employee employee) {
		if (employee == null) {
			logger.debug(ResultType.OBJECT_NULL);
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		if (employee.getEmpName() == null || employee.getEmpPinYinName() == null || employee.getEmpEnglishName() == null
				|| employee.getEmpeMail() == null || employee.getEmpPositionId() == null
				|| employee.getEmpMobile() == null || employee.getEmpStatus() == null || employee.getEmpTel() == null
				|| employee.getEmpUpdatedEmployeeName() == null) {
			logger.debug(ResultType.INPUT_NULL);
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		int lable = employeeDao.update(employee, employee.getEmpId());
		if(lable>0) {
			return ResponseResultUtil.success(ResultType.UPDATE_SUCCESS);
		}else {
			return ResponseResultUtil.error(ResultType.UPDATE_FAULT);
		}
	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#queryEmployee(java.lang.Integer)
	 */
	@Override
	public ResponseResult<Employee> queryEmployee(Integer id) {
		Employee employee = employeeDao.queryById(id);
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,employee);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#deleteEmployee(com.clps.dev.sms.entity.sm.Employee)
	 */
	@Override
	public ResponseResult<Employee> deleteEmployee(Integer id,Integer empStatus) {
		Employee employee = (Employee) queryEmployee(id).getData();
		if (employee == null) {
			logger.debug(ResultType.OBJECT_NULL);
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		employee.setEmpStatus(empStatus);
		int label = employeeDao.update(employee, employee.getEmpId());
		if(label>0) {
			return ResponseResultUtil.success(ResultType.DELETE_SUCCESS);
		}else {
			return ResponseResultUtil.error(ResultType.DELETE_FAULT);
		}

	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#queryAllEmployee()
	 */
	@Override
	public ResponseResult<Employee> queryAllEmployee() {
		List<Employee> list = employeeDao.queryALl();
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,list);
	}
	@Override
	public ResponseResult<Employee> queryAllEmployee(Integer currPage ,Integer pageSize,String time) {
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,queryByPageTime(currPage, pageSize, time));
	}

	/**
	 * 
	 * @see com.clps.dev.sms.service.sm.IEmployeeService#isUniqueOfEmail(java.lang.String)
	 */
	@Override
	public ResponseResult<Employee> isUniqueOfEmail(String email) {
		if(email==null) {
			logger.debug(ResultType.INPUT_NULL);
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		List<Employee> list = employeeDao.queryByEmail(email);
		if(list.size()>0) {
			return ResponseResultUtil.error(ResultType.QUERY_FAULT);
		}else {
			return ResponseResultUtil.success(ResultType.QUERY_SUCCESS);
		}
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPage(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public ResponseResult<Employee> queryByPage(Integer currPage, Integer pageSize) {
		PageBean<Employee> pageBean = queryByPageCommon(currPage, pageSize);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<Employee> list = employeeDao.findByPage(begin, pageSize);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPageTime(java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public ResponseResult<Employee> queryByPageTime(Integer currPage, Integer pageSize, String time,String empName,
			Integer status, Integer position) {
		PageBean<Employee> pageBean = queryByPageCommon(currPage, pageSize);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<Employee> list = employeeDao.findByPageOrderByTime(begin, pageSize, time,empName,status,position);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPageName(java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public ResponseResult<Employee> queryByPageName(Integer currPage, Integer pageSize, String name) {
		PageBean<Employee> pageBean = queryByPageCommon(currPage, pageSize);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<Employee> list = employeeDao.findByPageOrderByName(begin, pageSize, name);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}
	public PageBean<Employee> queryByPageCommon(Integer currPage, Integer pageSize) {
		PageBean<Employee> pageBean = new PageBean<>();
		pageBean.setCurrPage(currPage);
		pageBean.setPageSize(pageSize);
		Integer totalCount = employeeDao.findCount();
		pageBean.setTotalCount(totalCount);
		Double Count = totalCount.doubleValue();
		Integer totalPage = (int) Math.ceil(Count / pageSize);
		pageBean.setTotalPage(totalPage);
		return pageBean;
	}

}
